package com.simba.server.task;

import org.apache.log4j.Logger;
import com.simba.common.log.LogUtil;
import com.simba.server.components.SlotManager;
import com.simba.server.components.data.SlotInfo;

public class BoxInfoAllocTask extends Thread
{
	protected final static Logger logger = Logger.getLogger(BoxInfoAllocTask.class);

	public BoxInfoAllocTask()
	{
		this.setName("Box-Info-Alloc-Task");
	}

	@Override
	public void run()
	{
		try
		{
			this.doAllocBoxInfoForSlot();
		}
		catch (Exception ex)
		{
			if (ex instanceof InterruptedException)
			{
				Thread.currentThread().interrupt();
			}
			LogUtil.LogError(BoxInfoAllocTask.logger,
					"Catch an exception [" + ex.getMessage() + "]");
		}
	}

	private void doAllocBoxInfoForSlot()
	{
		try
		{
			SlotInfo[] slotInfos = SlotManager.getInstance().getSlotInfoList();
			for (SlotInfo slotInfo : slotInfos)
			{
				if (!slotInfo.getAllocBoxInfo())
				{
					this.doFetchBoxInfo(slotInfo);
					Thread.sleep(20);
				}
			}
		}
		catch (Exception ex)
		{
			LogUtil.LogError(BoxInfoAllocTask.logger,
					"[BoxInfoAllocTask.doAllocBoxInfo] Catch an exception [" + ex.getMessage()
							+ "]");
		}
	}

	private void doFetchBoxInfo(SlotInfo slotInfo)
	{
		/**
		 * 收容口不分配箱号
		 */
		Boolean res = SlotManager.getInstance().checkSlotIsTrash(slotInfo.getSlotId());
		if (res)
		{
			return;
		}

		SlotManager.getInstance().doAllocBoxInfo(slotInfo);
	}
}
